from PySide6.QtGui import QMouseEvent
from PySide6.QtWidgets import QWidget,QApplication
from PySide6.QtGui import QBitmap,QMouseEvent,QCursor,QPainter,QPixmap
from PySide6.QtCore import Qt,QTimer

import sys


class ShapWidget(QWidget):
    def __init__(self,parant=None):
        super(ShapWidget,self).__init__()
        self.setWindowTitle("不规则窗体")
        # self.i=1
        self.updatePix()
        # self.timer=QTimer()
        # self.timer.setInterval(1000)  #1000毫秒
        # self.timer.timeout.connect(self.timeChange)
        # self.timer.start()
        
    def updatePix(self):
        self.pix=QBitmap("./images/mask.png")
        self.resize(self.pix.size())
        self.setMask(self.pix)
        print(self.pix.size())
        self.dragPosition=None
        
    def mousePressEvent(self,event:QMouseEvent):
        if event.button() ==Qt.LeftButton:
            self.m_drag=True
            self.m_DragPosition=event.globalPosition().toPoint()-self.pos()
            event.accept()
            self.setCursor(QCursor(Qt.OpenHandCursor))
        if event.button()==Qt.RightButton:
            event.close()
            
    def mouseMoveEvent(self, event: QMouseEvent) -> None:
        if Qt.LeftButton and self.m_drag:
            #当左键移动窗体修改偏移值
            self.move(event.globalPosition().toPoint()-self.m_DragPosition)
            event.accept()
    def mouseReleaseEvent(self, event: QMouseEvent) -> None:
        self.m_drag=False
        self.setCursor(QCursor(Qt.ArrowCursor))
        
    #一般paintEvent在窗体首次绘制加载，要重新加载paintevent需要重新加载窗口使用
    #self.update() or self.repaint()
    
    def paintEvent(self,event):
        painter=QPainter(self)
        painter.drawPixmap(0,0,self.width(),self.height(),QPixmap("./images/boy.png"))
        
        
        
if __name__=="__main__":
    app=QApplication(sys.argv)
    win=ShapWidget()
    win.show()
    app.exec()
            